package com.haruhi.controller;

import com.haruhi.entity.FileDetail;
import com.haruhi.entity.UploadingFile;
import com.haruhi.entity.vo.ResponseEntity;
import com.haruhi.service.FileDetailService;
import com.haruhi.service.UploadingFileService;
import com.haruhi.utils.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class ResourceController {

    @Autowired
    private FileDetailService fileDetailService;

    @Autowired
    private UploadingFileService uploadingFileService;

    @GetMapping("/file_resource_list/{pageSize}/{current}")
    public ResponseEntity fileResourceList(@PathVariable Integer pageSize, @PathVariable Integer current){
        if( ParamUtil.isEmpty(pageSize) || ParamUtil.isEmpty(current)){
            return ResponseEntity.fail(-2,"参数错误");
        }

        FileDetail condition = new FileDetail();
        condition.setPageSize(pageSize);
        condition.setCurrent((current - 1) * pageSize);
        condition.setEnabled(true);
        List<FileDetail> byCondition = fileDetailService.getByCondition(condition);
        byCondition.forEach(c->{
            c.setIp(null);
            c.setRealName(null);
        });
        return ResponseEntity.success(0,"OK",byCondition);

    }
    @GetMapping("/file_resource/{id}")
    public ResponseEntity fileResourceById(@PathVariable Integer id){
        if(ParamUtil.isEmpty(id)){
            return ResponseEntity.fail(-2,"参数错误");
        }
        FileDetail condition = new FileDetail();
        condition.setId(id);
        List<FileDetail> byCondition = fileDetailService.getByCondition(condition);
        if(null == byCondition || byCondition.size() == 0){
            return ResponseEntity.fail(-3,"无数据");
        }
        FileDetail result = byCondition.get(0);
        result.setIp(null);
        result.setRealName(null);
        return ResponseEntity.success(result);
    }

    @GetMapping("/search/{active}")
    public ResponseEntity search(@RequestParam String keyword, @PathVariable Integer active){
        if(null == active || ParamUtil.isEmpty(keyword)){
            return ResponseEntity.fail(-2,"参数错误");
        }
        FileDetail condition = new FileDetail();
        // 1:文件名,2:上传人
        if(active.intValue() == 0){
            condition.setName(keyword);
        }else if (active.intValue() == 1){
            condition.setUploader(keyword);
        }
        condition.setEnabled(true);
        List<FileDetail> byCondition = fileDetailService.getByCondition(condition);
        byCondition.forEach(c->{
            c.setIp(null);
            c.setRealName(null);
        });
        return ResponseEntity.success(0,"OK",byCondition);
    }

    /**
     * 前端暂停上传时或组件销毁时
     * 更新上传到了哪一片
     * @param uploadingFile
     * @return
     */
    @PostMapping("/update_currentChunk")
    public ResponseEntity updateCurrentChunk(@RequestBody UploadingFile uploadingFile){
        if(null == uploadingFile || ParamUtil.isEmpty(uploadingFile.getMd5()) || ParamUtil.isEmpty(uploadingFile.getName())){
            return ResponseEntity.fail(-2,"参数错误");
        }
//        uploadingFileService.insert(uploadingFile);
        UploadingFile condition = new UploadingFile();
        condition.setMd5(uploadingFile.getMd5());
//        condition.setIdDel(false);
        uploadingFileService.updateChunkByMD5(uploadingFile,condition);
        return ResponseEntity.success(0,"OK");
    }
}
